<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
echo "<response>";
$agent_code = $_GET['agent_code'];
$action = $_GET['action'];


session_start();
if ($_SESSION['kteam_auth'] != true && ($_SESSION['agent_auth'] && !$action) == false)
{
    $expire=time()+60*60;
    setcookie("redirector", "agent.html", $expire);
    echo "<result>Login</result>";
    echo "</response>";
    //header("Location: login.html");
    exit;
}

include("sqlconnect.php");
include("sqlgenerator.php");
$debug = false;

if ($debug) {
    echo '<debug>';
    echo 'Post parameters: ' . $_GET['agent_code'];
    echo '</debug>';
}

if (!is_null($action) && $action=='delete') {
    db_connect();
    $query = delete_agent($agent_code);
    if ($debug) {
        echo '<debug>';
        echo 'Query: ' . $query;
        echo '</debug>';
    }

    $result = db_query($query);
    echo "<result>Deleted</result>";
    exit;
}

db_connect();

// Query the agent details from the agent code
if (is_null($agent_code)) {
    $query = get_all_agents();
} else {
    $query = get_agent($agent_code);
}
if ($debug) {
    echo '<debug>';
    echo 'Query: ' . $query;
    echo '</debug>';
}
$result = db_query($query);
if ($debug) {
    echo '<debug>';
    echo 'Query Result: ' . $result;
    echo '</debug>';
}

while ($row = mysql_fetch_array($result)) {
    echo '<agent>';
    try {
        echo '<agent_id>'.$row['id'].'</agent_id>';
        echo '<agent_name>'.$row['name'].'</agent_name>';
        echo '<agent_code>'.$row['code'].'</agent_code>';
        echo '<centre_code>'.$row['centre_code'].'</centre_code>';
        echo '<mobile_phone>'.$row['mobile_phone'].'</mobile_phone>';
        echo '<office_phone>'.$row['office_phone'].'</office_phone>';
        echo '<address>'.$row['address'].'</address>';
        echo '<password>'.$row['password'].'</password>';
    } catch (Exception $e) {
        echo '</agent>';
        echo "<result>Error</result>";
        echo "</response>";
    }
    echo '</agent>';
}
echo "<result>Success</result>";
echo "</response>";

?>